﻿using System;
using System.Reflection;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

namespace RealDolmen.SharePoint.Helpers
{
    public enum ListViewWebPartToolbarType 
    { 
        Full = 0, 
        Summary = 1, 
        NoToolbar = 2 
    }

    public static class SPListViewWebPartHelper
    {
        #region GetListViewWebPart
        public static Microsoft.SharePoint.WebPartPages.ListViewWebPart GetListViewWebPart(Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager, Guid g, string zoneId)
        {
            Microsoft.SharePoint.WebPartPages.ListViewWebPart listViewWebPart = null;
            if (!string.IsNullOrEmpty(zoneId))
            {
                for (int i = 0; i < manager.WebParts.Count && listViewWebPart == null; i++)
                {
                    WebPart webPart = manager.WebParts[i];
                    if (manager.GetZoneID(webPart) == zoneId)
                    {
                        listViewWebPart = (Microsoft.SharePoint.WebPartPages.ListViewWebPart)webPart;
                        if (listViewWebPart != null)
                        {
                            Guid temp = new Guid(listViewWebPart.ListName);
                            if (temp != g)
                                listViewWebPart = null;
                        }
                    }
                }
            }
            return listViewWebPart;
        } 
        #endregion // GetListViewWebPart

        #region SaveListViewWebPart
        // Thx to Joe Ferner
        // http://www.nearinfinity.com/blogs/joe_ferner/changing_the_toolbar_on_a_shar.html
        public static void SaveListViewWebPart(Microsoft.SharePoint.WebPartPages.ListViewWebPart lvwp, Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager limitedWebPartManager, ListViewWebPartToolbarType type)
        {
#pragma warning disable 618
            string storageKey = lvwp.StorageKey.ToString("B").ToUpper();
            string listId = lvwp.ListName;
            string viewId = lvwp.ViewGuid;
            uint toolbarType = (uint)type;

            // get the SPWebPartManager from SPLimitedWebPartManager    
            PropertyInfo webPartManagerProp = typeof(Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager).GetProperty("WebPartManager", BindingFlags.NonPublic | BindingFlags.Instance);
            Microsoft.SharePoint.WebPartPages.SPWebPartManager webPartManager = (Microsoft.SharePoint.WebPartPages.SPWebPartManager)webPartManagerProp.GetValue(limitedWebPartManager, null);
            // get the SPWebPartConnection from SPWebPartManager    
            PropertyInfo spWebPartsProp = typeof(Microsoft.SharePoint.WebPartPages.SPWebPartManager).GetProperty("SPWebParts", BindingFlags.NonPublic | BindingFlags.Instance);
            SPWebPartCollection webParts = (SPWebPartCollection)spWebPartsProp.GetValue(webPartManager, null);
            // Call the ApplyViewToListWebPart method on the SPWebPartConnection    
            // internal void ApplyViewToListWebPart(    
            //    string storageKey,    
            //    string listID,   
            //    string viewID,    
            //    uint toolbarType,    
            //    out uint flags)    
            MethodInfo applyViewToListWebPart = typeof(SPWebPartCollection).GetMethod("ApplyViewToListWebPart", BindingFlags.NonPublic | BindingFlags.Instance, null,
                new[]{        
                    typeof(string),        
                    typeof(string),        
                    typeof(string),        
                    typeof(uint),        
                    typeof(uint).MakeByRefType()      
                },
                null);
            object[] parameters = new object[5];
            parameters[0] = storageKey;
            parameters[1] = listId;
            parameters[2] = viewId;
            parameters[3] = toolbarType;
            applyViewToListWebPart.Invoke(webParts, parameters);
#pragma warning restore 618
        } 
        #endregion // SaveListViewWebPart
    }
}
